<?php
	require("libraries/student.php");
	class Links extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_link");
			$this->load->helper("form");
			$this->load->library("form_validation");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/links/index/';
			$config['total_rows'] = $this->model_link->count_all();
			$config['per_page'] = 5;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title'] = "Links exchange";
			$data['template'] = "links/list_link";
			$data['data']['links']= $this->model_link->listall($config['per_page'],$start);
			//$this->debug($data['data']['info']);
			$this->load->view("layout",$data);
		}
		public function subcont(){
			$config['base_url'] = base_url().'admin/links/subcont/';
			$config['total_rows'] = $this->model_link->count_all_sub();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title'] = "Links exchange";
			$data['template'] = "links/linkdetail/list_link";
			$data['data']['links']= $this->model_link->listall_sub($config['per_page'],$start);
			//$this->debug($data['data']['info']);
			$this->load->view("layout",$data);
		}
		public function add(){
		  	$data['title'] = "Links exchange";
			$data['template'] = "links/add_link";
			$data['data'] = "";
			if($this->input->post("ok") != ""){
				$value = array(
						"content_title" => $this->input->post("content_title"),
						"content_order" => $this->input->post("content_order"),
                        "content_des" => $this->input->post("content_des"),
						"content_key" => $this->input->post("content_key"),
						"content_date" => date("d/m/Y - H:i:s")
					);
				$this->model_link->add($value);
				redirect(base_url()."admin/links");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function addsub(){
		  	$data['title'] = "Links exchange";
			$data['template'] = "links/linkdetail/add_link";
			$data['data'] = "";
			$data['category'] = $this->model_link->listcate();
			if($this->input->post("ok") != ""){
				$value = array(
							"cont_title" => $this->input->post("cont_title"),
							"cont_link" => $this->input->post("cont_link"),
                            "cont_info" => $this->input->post("cont_info"),
							"content_id" => $this->input->post("content_id"),
						);
					$this->model_link->addsub($value);
					redirect(base_url()."admin/links/subcont");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title'] = "Links exchange";
			$data['template'] = "links/edit_link";
			$data['data'] = "";
			$data['link'] = $this->model_link->getdata($id);
			if($this->input->post("ok") != ""){
				//$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('in_name'))));
				$value = array(
				  			"content_title" => $this->input->post("content_title"),
							"content_order" => $this->input->post("content_order"),
                            "content_des" => $this->input->post("content_des"),
							"content_key" => $this->input->post("content_key"),
							"content_date" => date("d/m/Y - H:i:s")
						);
					$this->model_link->update($value,$id);
					redirect(base_url()."admin/links");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function updatesub(){
			$id = $this->uri->segment(4);
			$data['title'] = "Links exchange";
			$data['template'] = "links/linkdetail/edit_link";
			$data['data'] = "";
			$data['category'] = $this->model_link->listcate();
			$data['link'] = $this->model_link->getdatasub($id);
			if($this->input->post("ok") != ""){
				//$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('in_name'))));
				$value = array(
				  			"cont_title" => $this->input->post("cont_title"),
							"cont_link" => $this->input->post("cont_link"),
                            "cont_info" => $this->input->post("cont_info"),
							"content_id" => $this->input->post("content_id"),
						);
				///$this->debug($value);
				$this->model_link->updatesub($value,$id);
				redirect(base_url()."admin/links/subcont");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del($id){
			$id = $this->uri->segment(4);
			$this->model_link->del($id);
			redirect(base_url()."admin/links");
			exit();
		}
		public function delsub($id){
			$id = $this->uri->segment(4);
			$this->model_link->delsub($id);
			redirect(base_url()."admin/links/subcont");
			exit();
		}
	}